/**由数字、大写、小写、符号任意三种8到16个字符组成 */
export let checkPassword = (str) => {
    return /((^(?=.*[a-z])(?=.*[A-Z])(?=.*\W)[\da-zA-Z\W]{8,16}$)|(^(?=.*\d)(?=.*[A-Z])(?=.*\W)[\da-zA-Z\W]{8,16}$)|(^(?=.*\d)(?=.*[a-z])(?=.*\W)[\da-zA-Z\W]{8,16}$)|(^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[\da-zA-Z\W]{8,16}$))/.test(str)
}

/**是否包含中文 */
export let checkChinese = (str) => {
    return /^(?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/.test(str)
}

/**判断是否为移动端 */
export let isMobile = () => {
    return navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
}

/**深度拷贝对象 */
export let deepCopyObj = function (obj) {
    let zesult = Array.isArray(obj) ? [] : {}
    for (let key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
            if (typeof obj[key] === 'object' && obj[key] !== null) {
                zesult[key] = deepCopyObj(obj[key]) //递归复制
            }
            else { zesult[key] = obj[key] }
        }
    }
    return zesult
}

/**深度拷贝数组 */
export let deepCopyArray = function (source) {
    if (typeof source != "object") {
        return source;
    }
    if (source == null) {
        return source;
    }
    let newObj = source.constructor === Array ? [] : {};  //开辟一块新的内存空间
    for (let i in source) {
        newObj[i] = deepCopyArray(source[i]);
    }
    return newObj;
}

/**风速转等级文本 */
export let windSpeedToText = function (spd) {
    spd = parseFloat(spd)
    let speedtext = {}
    if (spd < 0.2) { speedtext.desc = 0 }
    else if (spd >= 0.2 && spd < 1.5) { speedtext.desc = 1 }
    else if (spd >= 1.5 && spd < 3.3) { speedtext.desc = 2 }
    else if (spd >= 3.3 && spd < 5.4) { speedtext.desc = 3 }
    else if (spd >= 5.4 && spd < 7.9) { speedtext.desc = 4 }
    else if (spd >= 7.9 && spd < 10.7) { speedtext.desc = 5 }
    else if (spd >= 10.7 && spd < 13.8) { speedtext.desc = 6 }
    else if (spd >= 13.8 && spd < 17.1) { speedtext.desc = 7 }
    else if (spd >= 17.1 && spd < 20.7) { speedtext.desc = 8 }
    else if (spd >= 20.7 && spd < 24.4) { speedtext.desc = 9 }
    else if (spd >= 24.4 && spd < 28.4) { speedtext.desc = 10 }
    else if (spd >= 28.4 && spd < 32.6) { speedtext.desc = 11 }
    else if (spd >= 32.6 && spd < 36.9) { speedtext.desc = 12 }
    else if (spd >= 36.9 && spd < 41.4) { speedtext.desc = 13 }
    else if (spd >= 41.4 && spd < 46.1) { speedtext.desc = 14 }
    else if (spd >= 46.1 && spd < 50.9) { speedtext.desc = 15 }
    else if (spd >= 50.9 && spd < 56) { speedtext.desc = 16 }
    else if (spd >= 56) { speedtext.desc = 17 }
    return speedtext.desc
}

/**角度转文本 */
export let angle2text = function (v) {
    v = parseFloat(v)
    let angletext = {}
    if (!(v >= 0 && v <= 360)) {
        angletext.desc = '未知'
        angletext.center = 0
    } else if (v >= 348.75 || v < 11.25) {
        angletext.desc = '北'
        angletext.center = 0
    } else if (v >= 11.25 && v < 33.75) {
        angletext.desc = '北偏东'
        angletext.center = 22.5
    } else if (v >= 33.75 && v < 56.25) {
        angletext.desc = '东北'
        angletext.center = 45
    } else if (v >= 56.25 && v < 78.75) {
        angletext.desc = '东偏北'
        angletext.center = 67.5
    } else if (v >= 78.75 && v < 101.25) {
        angletext.desc = '东'
        angletext.center = 90
    } else if (v >= 101.25 && v < 123.75) {
        angletext.desc = '东偏南'
        angletext.center = 112.5
    } else if (v >= 123.75 && v < 146.25) {
        angletext.desc = '东南'
        angletext.center = 135
    } else if (v >= 146.25 && v < 168.75) {
        angletext.desc = '南偏东'
        angletext.center = 157.5
    } else if (v >= 168.75 && v < 191.25) {
        angletext.desc = '南'
        angletext.center = 180
    } else if (v >= 191.25 && v < 213.75) {
        angletext.desc = '南偏西'
        angletext.center = 202.5
    } else if (v >= 213.75 && v < 236.25) {
        angletext.desc = '西南'
        angletext.center = 225
    } else if (v >= 236.25 && v < 258.75) {
        angletext.desc = '西偏南'
        angletext.center = 247.5
    } else if (v >= 258.75 && v < 281.25) {
        angletext.desc = '西'
        angletext.center = 270
    } else if (v >= 281.25 && v < 303.75) {
        angletext.desc = '西偏北'
        angletext.center = 295.5
    } else if (v >= 303.75 && v < 326.25) {
        angletext.desc = '西北'
        angletext.center = 315
    } else if (v >= 326.25 && v < 348.75) {
        angletext.desc = '北偏西'
        angletext.center = 337.5
    }
    angletext.center = v
    return angletext
}

/**转换风场UV值 */
export let computeWindUV = (u_value, v_value) => {
    let value = Math.sqrt(u_value * u_value + v_value * v_value);
    let angle = Math.acos(u_value / value);
    if (v_value < 0) {
        angle = -angle;
    }
    angle = Math.round(angle / Math.PI * 180.0 + 90);
    if (angle < 0) {
        angle = angle + 360;
    }
    angle = Math.round(360 - angle);
    value = value.toFixed(1);
    return {
        value: parseFloat(value),
        angle: angle
    }
}

/**转换流场UV值*/
export let UV2SDCur = function (u, v) {
    let spd, dir
    if (u === 0 && v === 0) {
        spd = 0
        dir = -9999
        return { speed: spd, dir: dir }
    }

    if (u === 0 || v === 0) {
        if (u === 0) {
            spd = v < 0 ? -v : v
            if (v > 0) { dir = Math.PI / 2 }
            else if (v < 0) { dir = Math.PI * 3 / 2 }
        } else if (v === 0) {
            spd = u < 0 ? -u : u
            if (u > 0) { dir = 0 }
            else if (u < 0) { dir = Math.PI }
        }
        dir = (2.5 * Math.PI - dir) % (Math.PI * 2)
        return { speed: spd, dir: dir }
    }

    spd = Math.sqrt(u * u + v * v)
    dir = Math.atan(u / v)
    if (u < 0 && v > 0) { dir = 1 / 2.0 * Math.PI - dir }
    else if (u < 0 && v < 0) { dir = 3 / 2.0 * Math.PI - dir }
    else if (u > 0 && v < 0) { dir = 3 / 2.0 * Math.PI - dir }
    else if (u > 0 && v > 0) { dir = 1 / 2.0 * Math.PI - dir }
    dir = (2.5 * Math.PI - dir) % (Math.PI * 2)
    return { speed: spd, dir: dir }
}

/**[地图数值]转[数据数值] */
export let convertLonlat1 = function (latlng) {
    //由leaflet数值转为0-360的范围
    let lat = latlng.lat
    let lng = latlng.lng
    lng = lng % 360
    if (lng < 0) { lng = lng + 360 }
    return [lat, lng]
}

/**[地图数值]转[南北纬] */
export let convertLonlat2 = function (latlng) {
    let lat = latlng.lat
    let lng = latlng.lng
    lng = lng % 360
    if (lng < 0) { lng = lng + 360 }
    lng = ((lng < 180) ? (lng.toFixed(3) + "°E") : (360 - lng).toFixed(3) + "°W")
    lat = (lat > 0 ? lat.toFixed(3) + "°N" : -lat.toFixed(3) + "°S")
    return lat + " " + lng
}

/**[地图数值]转[南北纬] */
export let convertLonlat3 = function (latlng) {
    let lat = latlng.lat
    let lng = latlng.lng
    lng = lng % 360
    if (lng < 0) { lng = lng + 360 }
    lng = ((lng < 180) ? ("东经 " + lng.toFixed(3) + "°") : ("西经 " + (360 - lng).toFixed(3) + "°"))
    lat = (lat > 0 ? ("北纬 " + lat.toFixed(3) + "°") : ("南纬 " + -lat.toFixed(3) + "°"))
    return [lat, lng]
}

/**[地图数值]转[南北纬] */
export let convertLonlat4 = function (latlng) {
    let lat = latlng.lat
    let lng = latlng.lng
    lng = lng % 360
    if (lng < 0) { lng = lng + 360 }
    lng = (lng < 180) ? ("经度:" + lng.toFixed(3) + "°E") : ("经度:" + (360 - lng).toFixed(3) + "°W")
    lat = (lat > 0) ? ("纬度:" + lat.toFixed(3) + "°N") : ("纬度:" + -lat.toFixed(3) + "°S")
    return [lat, lng]
}

export let getHexColor = function (color) {
    let values = color
        .replace(/rgba?\(/, '')
        .replace(/\)/, '')
        .replace(/[\s+]/g, '')
        .split(',')
    let a = parseFloat(values[3] || 1),
        r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255),
        g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255),
        b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255)
    return '#' +
        ('0' + r.toString(16)).slice(-2) +
        ('0' + g.toString(16)).slice(-2) +
        ('0' + b.toString(16)).slice(-2)
}

export let getBrightDark = function (color) {
    let values = color
        .replace(/rgba?\(/, '')
        .replace(/\)/, '')
        .replace(/[\s+]/g, '')
        .split(',')
    let a = parseFloat(values[3] || 1),
        r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255),
        g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255),
        b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255)
    let threshold = (0.299 * r + 0.587 * g + 0.114 * b)
    return threshold > 128 ? '#000' : '#fff'
}

export let isNumber = (val) => {
    val = parseFloat(val)
    let regPos = /^\d+(\.\d+)?$/; //非负浮点数
    let regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
        return true;
    } else {
        return false;
    }
}

export let wait = (ms) => {
    return new Promise((resolve) => {
        setTimeout(() => { resolve(ms) }, ms)
    })
}